#!/bin/bash

# copy program files
echo 'Copying program files...'
cp -r ./usr/local/fivedash/ /usr/local/

# make cgi scripts executable
echo  'Editing permissions on cgi scripts...'
chmod -R 0755 /usr/local/fivedash/cgi/
chown -R www-data:www-data /usr/local/fivedash/cgi/phunc/tp_index/

CRYPTPW=$(perl -e 'print crypt("qer","password")')
sed -e "s/{{password}}/$CRYPTPW/" /usr/local/fivedash/sql/fivedash.sql > /usr/local/fivedash/sql/fivedash.sql.tmp1
sed -e "s/{{username}}/fivedash/" /usr/local/fivedash/sql/fivedash.sql.tmp1 > /usr/local/fivedash/sql/fivedash.sql.tmp2
rm -f /usr/local/fivedash/sql/fivedash.sql
rm -f /usr/local/fivedash/sql/fivedash.sql.tmp1
mv /usr/local/fivedash/sql/fivedash.sql.tmp2 /usr/local/fivedash/sql/fivedash.sql
CONTRIB='\/usr\/share\/postgresql\/8.2\/contrib'
sed -e "s/{{contrib}}/$CONTRIB/" /usr/local/fivedash/sql/createdb.sql > /usr/local/fivedash/sql/createdb.sql.tmp1
sed -e "s/{{password}}/hushhush/" /usr/local/fivedash/sql/createdb.sql.tmp1 > /usr/local/fivedash/sql/createdb.sql.tmp2
rm /usr/local/fivedash/sql/createdb.sql
rm /usr/local/fivedash/sql/createdb.sql.tmp1
mv /usr/local/fivedash/sql/createdb.sql.tmp2 /usr/local/fivedash/sql/createdb.sql
sed -e "s/{{password}}/hushhush/" /usr/local/fivedash/cgi/phunc/__init__.py > /usr/local/fivedash/cgi/phunc/__init__.py.tmp
rm /usr/local/fivedash/cgi/phunc/__init__.py
mv /usr/local/fivedash/cgi/phunc/__init__.py.tmp /usr/local/fivedash/cgi/phunc/__init__.py

# apache config
echo 'Adding Apache site config...'
sed -e "s/{{apache2_path}}/\/etc\/apache2/" ./etc/apache2/sites-available/fivedash-ssl > /etc/apache2/sites-available/fivedash-ssl
ln -s /etc/apache2/sites-available/fivedash-ssl /etc/apache2/sites-enabled/
#allow upload
chown -R  www-data:www-data /var/fivedash
# restart apache
echo -n 'Restart Apache? (y/n):'
read  ANS
if [ $ANS = 'y' ]; then
	/etc/init.d/apache2 restart
fi
echo 'Installation complete.'
